<template>
  <el-container class="main-container">
    <!-- 头部区域 -->
    <el-header>
      <!-- 左侧的 logo -->
      <img src="../../assets/images/logo.png" alt="" />
      <!-- 右侧的菜单 -->
      <el-menu
        class="el-menu-top"
        mode="horizontal"
        background-color="#23262E"
        text-color="#fff"
        active-text-color="#409EFF"
      >
        <el-submenu index="1">
          <template slot="title">
            <!-- 头像 -->
            <img src="../../assets/logo.png" alt="" class="avatar" />
            <span>个人中心</span>
          </template>
          <el-menu-item index="1-1"
            ><i class="el-icon-s-operation"></i>基本资料</el-menu-item
          >
          <el-menu-item index="1-2"
            ><i class="el-icon-camera"></i>更换头像</el-menu-item
          >
          <el-menu-item index="1-3"
            ><i class="el-icon-key"></i>重置密码</el-menu-item
          >
        </el-submenu>
        <el-menu-item @click="logout" index="2"
          ><i class="el-icon-switch-button"></i>退出</el-menu-item
        >
      </el-menu>
    </el-header>
    <el-container>
      <!-- 侧边栏区域 -->
      <el-aside width="200px">
        <div class="user-box">
          <!-- webpack 处理图片的注意事项: 如果 src 里面写的是变量, webpack 不会进行打包, 写死会打包 -->
          <!-- <img :src="userInfo.user_pic || '../../assets/logo.png'" alt="" /> -->
          <img v-if="userInfo.user_pic" :src="userInfo.user_pic" />
          <img v-else src="../../assets/logo.png" />
          <span>欢迎 {{ userInfo.nickname || userInfo.username }} </span>
        </div>

        <!-- 侧边菜单 -->
        <!-- $route 路由参数对象, 用于接收传递的路由参数 -->
        <el-menu
          router
          :default-active="$route.path"
          class="el-menu-vertical-demo"
          background-color="#23262E"
          text-color="#fff"
          active-text-color="#409EFF"
        >
          <!--
            导航项有两种情况:
            1. 有子菜单的导航项
              el-submenu 组件包裹 el-menu-item 的就是有子菜单的
            2. 无子菜单的导航项
              就是一个 el-menu-item

            el-menu 的属性 default-active 控制默认高亮的导航项
            导航项的 index 属性, 用于做当前导航项的唯一标识, 只能用 string 类型的值
           -->
          <!-- <ul>
            <li></li>
            <p></p>
          </ul> -->
          <!-- template 是一个虚拟标签, 最终不会在 DOM 上渲染任何标签, 只起到了包裹的作用 -->
          <!-- 应用场景: 当循环渲染的标签有多种类型时使用 -->
          <template v-for="item in menus">
            <el-menu-item :index="item.indexPath" v-if="!item.children" :key="item.indexPath">
              <i :class="item.icon"></i>
              <span slot="title">{{ item.title }}</span>
            </el-menu-item>
            <el-submenu :index="item.indexPath" v-else :key="item.indexPath">
              <template #title>
                <i :class="item.icon"></i>
                <span>{{ item.title }}</span>
              </template>
              <el-menu-item v-for="child in item.children" :key="child.indexPath" :index="child.indexPath">
                <i :class="child.icon"></i>
                {{ child.title }}
                </el-menu-item>
            </el-submenu>
          </template>
        </el-menu>
      </el-aside>
      <el-container>
        <!-- 页面主体区域 -->
        <el-main>
          <router-view></router-view>
        </el-main>
        <!-- 底部 footer 区域 -->
        <el-footer>© www.itheima.com - 黑马程序员</el-footer>
      </el-container>
    </el-container>
  </el-container>
</template>

<script>
import { mapState } from 'vuex'

export default {
  name: 'Main',
  data () {
    return {
      menus: []
    }
  },
  async created () {
    // console.log(this.$route.path)
    // 发送 ajax 请求, 获取 菜单列表
    const { data: res } = await this.$http.get('/my/menus')

    if (res.code !== 0) return
    this.menus = res.data

    // 调用 actions 的函数发请求获取数据
    this.$store.dispatch('user/getUserInfo')
  },
  methods: {
    logout () {
      this.$confirm('真的要退出吗?嘤嘤嘤?', '确认退出?', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          // 提示用户退出成功啦
          this.$message({
            type: 'success',
            message: '退出登录成功!呜呜呜!'
          })
          // 清空 token
          this.$store.commit('user/updateToken', '')
          // 跳转至登录页
          this.$router.push('/login')
        })
        .catch(() => {})
    }
  },
  computed: {
    ...mapState('user', ['userInfo', 'token'])
  }
}
</script>

<style lang="less" scoped>
// 左侧边栏用户信息区域
.user-box {
  height: 70px;
  display: flex;
  justify-content: center;
  align-items: center;
  border-top: 1px solid #000;
  border-bottom: 1px solid #000;
  user-select: none;
  img {
    width: 35px;
    height: 35px;
    border-radius: 50%;
    background-color: #fff;
    margin-right: 15px;
    object-fit: cover;
  }
  span {
    color: white;
    font-size: 12px;
  }
}

// 侧边栏菜单的样式
.el-aside {
  .el-submenu,
  .el-menu-item {
    width: 200px;
    user-select: none;
  }
}

.main-container {
  height: 100%;
  .el-header,
  .el-aside {
    background-color: #23262e;
  }
  .el-header {
    padding: 0;
    display: flex;
    justify-content: space-between;
  }
  .el-main {
    overflow-y: scroll;
    height: 0;
    background-color: #f2f2f2;
  }
  .el-footer {
    background-color: #eee;
    font-size: 12px;
    display: flex;
    justify-content: center;
    align-items: center;
  }
}

.avatar {
  border-radius: 50%;
  width: 35px;
  height: 35px;
  background-color: #fff;
  margin-right: 10px;
  object-fit: cover;
}
</style>
